##### Replication File for Plots
##### "Respect the Process: The Public Cost of Unilateral Action in Comparative Perspective"
##### Jonathan Chu and Scott Williamson
##### October 2023
##### For publication in The Journal of Politics

##### Important note: all coefficients for the plots below are taken from regressions implemented
##### in jop_appendix_replication.dta.

##### Load packages

library(readstata13)
library(ggplot2)
library(ggthemes)
library(grid)
library(gridExtra)
library(psych)
library(car)
library(Hmisc)
library(dotwhisker)
library(dplyr)
library(tidyr)
library(broom)
library(egg)
library(factoextra)

##### Code for shared legends

grid_arrange_shared_legend <- function(..., ncol = length(list(...)), nrow = 1, position = c("bottom", "right")) {
  
  plots <- list(...)
  position <- match.arg(position)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  lwidth <- sum(legend$width)
  gl <- lapply(plots, function(x) x + theme(legend.position="none"))
  gl <- c(gl, ncol = ncol, nrow = nrow)
  
  combined <- switch(position,
                     "bottom" = arrangeGrob(do.call(arrangeGrob, gl),
                                            legend,
                                            ncol = 1,
                                            heights = unit.c(unit(1, "npc") - lheight, lheight)),
                     "right" = arrangeGrob(do.call(arrangeGrob, gl),
                                           legend,
                                           ncol = 2,
                                           widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
  
  grid.newpage()
  grid.draw(combined)
  
  # return gtable invisibly
  invisible(combined)
  
}

##### Set Working Directory

#setwd("")


###### Main Paper Plots


### Figure 1: Main Effects

main_data<-read.csv("main_effects_data.csv")
head(main_data)

main_effect_plot<-dwplot(main_data, dot_args = list(size = 3.5),whisker_args = list(size = 1.2)) + 
  theme_bw() + #geom_point(size = 3, shape = 16) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("") +
  scale_x_continuous("",limits = c(-1, 0.2)) + theme(panel.spacing = unit(1, "lines")) +
  theme(legend.title=element_blank(),axis.title.x=element_text(size=10),
        legend.text=element_text(size=10),legend.position="bottom",
        legend.margin=margin(-20,0,0,0),
        plot.margin=margin(.2,12,.2,-12),
        axis.text.x=element_text(size=10,color="black"),
        axis.text.y=element_text(size=10,color="black")) + scale_color_manual(values=c("grey70", "grey44", "black"))
main_effect_plot
#ggsave("main_effect_plot.jpg",main_effect_plot, scale = 1, width = 6, height = 4, units = "in", dpi = 500)


### Figure 2: Partisan Subgroup Effects

# USA
partisan_usa_data<-read.csv("partisan_usa_data.csv")
head(partisan_usa_data)

partisan_usa_plot<-dwplot(partisan_usa_data, dot_args = list(size = 3.5),whisker_args = list(size = 1.2)) + 
  theme_bw() + #geom_point(size = 3, shape = 16) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("") +
  scale_x_continuous("",limits = c(-1, 0.2)) + theme(panel.spacing = unit(1, "lines")) +
  theme(legend.title=element_blank(),axis.title.x=element_text(size=10),
        legend.text=element_text(size=10),legend.position="right",
        legend.margin=margin(-20,0,0,0),
        plot.margin=margin(.2,12,.2,-12),
        axis.text.x=element_text(size=10,color="black"),
        axis.text.y=element_text(size=10,color="black")) + scale_color_manual(values=c("grey70", "grey44", "black"))
partisan_usa_plot

# Egypt
partisan_egypt_data<-read.csv("partisan_egypt_data.csv")
head(partisan_egypt_data)

partisan_egypt_plot<-dwplot(partisan_egypt_data, dot_args = list(size = 3.5),whisker_args = list(size = 1.2)) + 
  theme_bw() + #geom_point(size = 3, shape = 16) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("") +
  scale_x_continuous("",limits = c(-1, 0.2)) + theme(panel.spacing = unit(1, "lines")) +
  theme(legend.title=element_blank(),axis.title.x=element_text(size=10),
        legend.text=element_text(size=10),legend.position="right",
        legend.margin=margin(-20,0,0,0),
        plot.margin=margin(.2,12,.2,-12),
        axis.text.x=element_text(size=10,color="black"),
        axis.text.y=element_text(size=10,color="black")) + scale_color_manual(values=c("grey70", "black"))
partisan_egypt_plot

# Combination of USA / Egypt Partisan Plots
partisan_plots<-ggarrange(partisan_usa_plot,partisan_egypt_plot, ncol=1)
partisan_plots
#ggsave("partisan_plots.jpg",partisan_plots, scale = 1, width = 6, height = 4, units = "in", dpi = 500)


### Figure 3: Mechanism Effects

mechanism_data<-read.csv("mechanism_data.csv")
head(mechanism_data)

mechanism_plot<-dwplot(mechanism_data, dot_args = list(size = 3.5),whisker_args = list(size = 1.2)) + 
  theme_bw() + #geom_point(size = 3, shape = 16) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("") +
  scale_x_continuous("",limits = c(-1.6, 0.5)) + theme(panel.spacing = unit(1, "lines")) +
  theme(legend.title=element_blank(),axis.title.x=element_text(size=10),
        legend.text=element_text(size=10),legend.position="bottom",
        legend.margin=margin(-20,0,0,0),
        plot.margin=margin(.2,12,.2,-12),
        axis.text.x=element_text(size=10,color="black"),
        axis.text.y=element_text(size=10,color="black")) + scale_color_manual(values=c("grey70", "grey44", "black"))
mechanism_plot
#ggsave("mechanism_plot_combined.jpg",mechanism_plot, scale = 1, width = 6, height = 4, units = "in", dpi = 500)


### Figure 4: Variation by Opinion Poll Treatment

poll_data<-read.csv("poll_data.csv")
head(poll_data)

poll_plot<-dwplot(poll_data, dot_args = list(size = 3.5),whisker_args = list(size = 1.2)) + 
  theme_bw() + #geom_point(size = 3, shape = 16) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("") +
  scale_x_continuous("",limits = c(-1, 0.2)) + theme(panel.spacing = unit(1, "lines")) +
  theme(legend.title=element_blank(),axis.title.x=element_text(size=10),
        legend.text=element_text(size=10),legend.position="bottom",
        legend.margin=margin(-20,0,0,0),
        plot.margin=margin(.2,12,.2,-12),
        axis.text.x=element_text(size=10,color="black"),
        axis.text.y=element_text(size=10,color="black")) + scale_color_manual(values=c("grey70", "black"))
poll_plot
#ggsave("poll_plot.jpg",poll_plot, scale = 1, width = 6, height = 4, units = "in", dpi = 500)


### Figure 5: Importance of Democracy Subgroup Effects

dem_data<-read.csv("dem_important_data.csv")
head(dem_data)

dem_plot<-dwplot(dem_data, dot_args = list(size = 3.5),whisker_args = list(size = 1.2)) + 
  theme_bw() + #geom_point(size = 3, shape = 16) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("") +
  scale_x_continuous("",limits = c(-1.2, 0.2)) + theme(panel.spacing = unit(1, "lines")) +
  theme(legend.title=element_blank(),axis.title.x=element_text(size=10),
        legend.text=element_text(size=10),legend.position="bottom",
        legend.margin=margin(-20,0,0,0),
        plot.margin=margin(.2,12,.2,-12),
        axis.text.x=element_text(size=10,color="black"),
        axis.text.y=element_text(size=10,color="black")) + scale_color_manual(values=c("grey70", "grey44", "black"))
dem_plot
#ggsave("dem_plot.jpg",dem_plot, scale = 1, width = 6, height = 4, units = "in", dpi = 500)


### Figure A.8 from SI: Barometer Country-Level Correlations

cross<-read.csv("descriptive-country-correlations.csv")
head(cross)
cross$Protest<-100-cross$No_Protest
head(cross)

# Approval of Executive
approve_country_plot<- ggplot(cross, aes(x = Legislature_Weak, y = Approve_Executive, color=Barometer)) +
  theme_bw() + geom_point(size=2) + ggtitle("Approval of Executive") +
  scale_color_manual(values=c("#E69F00","chocolate3","#999999","gray10","#56B4E9","steelblue3","navyblue")) + xlab("Believe Legislative Influence is Low (%)") +
  scale_y_continuous("Approve of Executive (%)",limits = c(0, 100)) +
  theme(legend.text=element_text(size=10),legend.title=element_text(size=10),
        axis.title.y=element_text(size=10,color="black"),axis.text.y=element_text(size=10,color="black"),
        axis.title.x=element_text(size=10,color="black"),axis.text.x=element_text(size=10,color="black"),title=element_text(size=10))
approve_country_plot

# Protest
protest_country_plot<-ggplot(cross, aes(x = Legislature_Weak, y = Protest, color=Barometer)) +
  theme_bw() + geom_point(size=2) + ggtitle("Willingness to Protest") +
  scale_color_manual(values=c("#E69F00","chocolate3","#999999","gray10","#56B4E9","steelblue3","navyblue")) + xlab("Believe Legislative Influence is Low (%)") +
  scale_y_continuous("Protest (%)",limits = c(0, 100)) +
  theme(legend.text=element_text(size=10),legend.title=element_text(size=10),
        axis.title.y=element_text(size=10,color="black"),axis.text.y=element_text(size=10,color="black"),
        axis.title.x=element_text(size=10,color="black"),axis.text.x=element_text(size=10,color="black"),title=element_text(size=10))
protest_country_plot

# Democracy
dem_country_plot<- ggplot(cross, aes(x = Legislature_Weak, y = Democracy, color=Barometer)) +
  theme_bw() + geom_point(size=2) + ggtitle("Rating of Democracy") +
  scale_color_manual(values=c("#E69F00","chocolate3","#999999","gray10","#56B4E9","steelblue3","navyblue")) + xlab("Believe Legislative Influence is Low (%)") +
  scale_y_continuous("Country is Democratic (%)",limits = c(0, 100)) +
  theme(legend.text=element_text(size=10),legend.title=element_text(size=10),
        axis.title.y=element_text(size=10,color="black"),axis.text.y=element_text(size=10,color="black"),
        axis.title.x=element_text(size=10,color="black"),axis.text.x=element_text(size=10,color="black"),title=element_text(size=10))
dem_country_plot

# Combined Plots
descriptive_country_plots<-grid_arrange_shared_legend(approve_country_plot,protest_country_plot,dem_country_plot,ncol=3)
#ggsave("descriptive_country_plots.jpg",descriptive_country_plots, scale = 1, width = 10, height = 6, units = "in", dpi = 500)
